layui.use(['table','jquery','layer','form'], function(){
  var table = layui.table,
  		$ = layui.$,
  		layer=layui.layer,
  		form=layui.form;
  var perpissionLayer = null;
  //第一个实例
  table.render({
    elem: '#permissionList'
    ,height:'380'	
    ,url: $("#contextPath").val() + "/auth/permission/queryPermissionList" //数据接口
    ,page: true //开启分页
    ,id:'permissionList'
    ,cols: [[ //表头
       {type:'checkbox',field:"id"}
       ,{type:'numbers',title:"序号"}
      ,{field: 'permissionCode', title: '权限Code',align:'center', width:200}
      ,{field: 'permissionName', title: '权限名称',align:'center', width:150}
      ,{field: 'permissionDesc', title: '权限描述',align:'center', width:150}
      ,{field: "createUser", title: '创建用户',align:'center', width:100,templet:function(d){
    	  if(d.createUser != null) {
    		  return d.createUser.userName;
    	  } else {
    		  return "";
    	  }
    	  
      }}
      ,{field: 'createDate', title: '创建时间', align:'center',width:160,templet:function(d){
    	  return convertDate(d.createDate,"1");
      }}
      ,{field: 'updateUser', title: '更新用户',align:'center', width:100,templet:function(d){
    	  if(d.updateUser != null) {
    		  return d.updateUser.userName;
    	  } else {
    		  return "";
    	  }
    	  
      }}
      ,{field: 'updateDate', title: '更新时间', align:'center',width:160,templet:function(d){
    	  return convertDate(d.updateDate,"1");
      }}
       ,{fixed: 'right', title:'操作' , width:160, align:'center', toolbar: '#barDemo'} //这里的toolbar值是模板元素的选择器
    ]]
  });
  $("#addPermission").on("click",function(){
	  layui.$("#id").parent().parent().hide();
	  layui.$("#id").val("");
	  layui.$("#permissionCode").val("");
	  layui.$("#permissionName").val("");
	  layui.$("#permissionDesc").val("");
	  perpissionLayer = layer.open({
			 type:"1",
			 title:"新增权限",
			 content:$("#permissionForm"),
			 area:['100%', '100%'],
		     anim: 2 //显示动画
		  });
  });
  $("#editPermission").on("click",function(){
	  var checkStatus = table.checkStatus("permissionList");
	  if (checkStatus.data.length==0) {
		  layer.alert("请至少选择一条数据",{icon:'0'});
	  } else if (checkStatus.data.length>1) {
		  layer.alert("至多选择一条数据",{icon:'0'});
	  } else {
		  layui.$("#id").parent().parent().show();
		  perpissionLayer = layer.open({
			 type:"1",
			 title:"编辑权限",
			 content:$("#permissionForm"),
			 area:['420px']
		  });
		  $("#id").val(checkStatus.data[0].id);
		  $("#permissionCode").val(checkStatus.data[0].permissionCode);
		  $("#permissionName").val(checkStatus.data[0].permissionName);
		  $("#permissionDesc").val(checkStatus.data[0].permissionDesc);
	  }
  });
  
  /**
   * 删除权限
   */
  $("#deletePermission").on("click",function(){
	  var checkStatus = table.checkStatus("permissionList");
	  if (checkStatus.data.length==0) {
		  layer.alert("请至少选择一条数据",{icon:'0'});
	  } else if (checkStatus.data.length>=1) {
		  layer.confirm('确认删除吗?', {icon: 3, title:'提示'}, function(index){
			  var idList = [];
			  $.each(checkStatus.data,function(index,item){
				  idList.push(item.id);
			  });
			  $.ajax({
					 method:"post",
					 url:$("#contextPath").val()+"/auth/permission/delete",
					 contentType: 'application/json;charset=utf-8',
					 data:JSON.stringify(idList),
					 success:function(data) {
						 if(data =="1") {
							 successMsg("删除成功");
							 table.reload("permissionList",{
								 url: $("#contextPath").val() + "/auth/permission/queryPermissionList"
							 });
						 } else {
							 errorMsg("删除失败");
						 }
						 
					 },
					 error : function(data) {
						 errorMsg("删除失败");
					 }
			  });
			  layer.close(index);
			});
	  } else {
		  
	  }
  });
  
  form.on("submit(save)",function(data){
	  $.ajax({
			 method:"post",
			 url:$("#contextPath").val()+"/auth/permission/save",
			 contentType: 'application/json;charset=utf-8',
			 data:JSON.stringify(data.field),
			 success:function(data) {
				 if(data =="1") {
					 layer.msg("保存成功",{
						 icon:"6",
						 time:"1500"
					 });
				 } else {
					 layer.msg("保存失败",{
						 icon:"5",
						 time:"1500"
					 });
				 }
				 layer.close(perpissionLayer);
				 table.reload("permissionList",{
					 url: $("#contextPath").val() + "/auth/permission/queryPermissionList"
				 });
			 },
			 error:function(data){
				 layer.msg("保存失败",{
					 icon:"5",
					 time:"1500"
				 });
			 }
		 });
	  return false;
  });
  
  //验证规则
  form.verify({
	  notExist:function(value,item){
		  var msg = "";
		  $.ajax({
				 method:"post",
				 url:$("#contextPath").val()+"/auth/permission/check",
				 contentType: 'application/json;charset=utf-8',
				 async: false,
				 data:JSON.stringify({
					 "id":$("#id").val(),
					 "permissionCode":value
				 }),
				 success:function(data) {
					 if (data == "-1") {
						 msg = "权限Code已存在，请重新输入!";
					 } else if (data == "-2") {
						 msg = "系统异常，请稍后!";
					 }
				 },
				 error :function(data) {
					 msg = "系统异常，请稍后!";
				 }
		  });
		  if (msg != "") {
			  return msg;
		  }
	  }
  });
  
});